package com.taptech.doufu.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.taptech.doufu.R
import com.taptech.doufu.model.novel.bean.NovelRecommendBean
import com.taptech.doufu.ui.activity.NovelDetailsActivity
import com.taptech.doufu.ui.activity.story.StoryDetailsActivity
import com.taptech.doufu.util.GlideRoundTransform
import com.taptech.doufu.util.GlideUtil
import com.taptech.doufu.util.ScreenUtil
import com.taptech.doufu.weex.image.TFImageViewUtil
import kotlinx.android.synthetic.main.adapter_novel_recommend.view.*

class NovelRecommendGridAdapter(context:Context,val count:Int): BaseRecyclerAdappter<NovelRecommendBean,NovelRecommendGridAdapter.ViewHolder>(context){

    private var coverWidth:Int = (ScreenUtil.getScreenWidth(context)-ScreenUtil.dip2px(context,60f))/count
    private var coverHeight:Int

    init {
        coverHeight = (coverWidth/0.75f).toInt()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.adapter_novel_recommend,null)
        val holder = ViewHolder(view)
        holder.itemView.ivCover.layoutParams = RelativeLayout.LayoutParams(coverWidth,coverHeight)
        holder.itemView.tvTitle.maxWidth = coverWidth
        return holder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val item = dataList[position]
        holder.itemView.tvTitle.text = item.topic_title

        GlideUtil.displayRoundImage(holder.itemView.ivCover, if (item.with_image.startsWith("http")) item.with_image else "https://imgold.doufu.la/${item.with_image}",5,R.drawable.img_default_loading7)
        holder.itemView.setOnClickListener {
            if (item.single_page == 1) {
                StoryDetailsActivity.startActivity(mContext,item.id)
            }else{
                NovelDetailsActivity.startActivity(mContext,item.id)
            }
        }
    }

    class ViewHolder(view: View): RecyclerView.ViewHolder(view)
}